﻿#include "RetrunChargeGun.h"
#include "ui_retrunchargegun.h"

RetrunChargeGun::RetrunChargeGun(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RetrunChargeGun)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");
#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif
    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    for(int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("10");
        connect(this->countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
    }
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));
}

RetrunChargeGun::~RetrunChargeGun()
{
    delete ui;
}

void RetrunChargeGun::setCountHide(int current)
{
    if (current == returnChargeGunPage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        if(!countdown[focus_gun_-1]->OneSecound->isActive())
            countdown[focus_gun_-1]->CountDownStart();
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}

void RetrunChargeGun::countdownOver()
{
    int charging_gun = GUN_AMOUNT;

    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i< GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])
        {
//            Refreash_Thread[i].swipCardflag = 0;
            Refreash_Thread[i].swipCardflag = 0;
            Refreash_Thread[i].appcharge = 0;
            Refreash_Thread[i].VINcharge = 0;
            Refreash_Thread[i].swipcard_YCT = 0;
            GUN_STATE[i].VINflag = 0;
            GUN_STATE[i].Page = chargeGunNotConnectPage;
            if(!Refreash_Thread[i].ErrorTimer->isActive())
                Refreash_Thread[i].ErrorTimer->start(300);
        }
        // get gun which is in charging!
        if (((GUN_STATE[i].Page == chargeWatchingDCPage)
            || (GUN_STATE[i].Page == chargeWatchingACPage))
            && (charging_gun >= GUN_AMOUNT))
        {
            charging_gun = i;
        }
    }

    // jump to charging info page!
    if (charging_gun < GUN_AMOUNT)
    {
        focus_gun_ = charging_gun + 1;  // select charging gun!
        stackLayout->setCurrentIndex(GUN_STATE[charging_gun].Page);
    }
}
void RetrunChargeGun::returnChargeGunCountdownStartSlot(int GUN_NO)
{
    countdown[GUN_NO]->CountDownStart();
}

void RetrunChargeGun::on_buttonOk_clicked()
{
    int charging_gun = GUN_AMOUNT;

    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = chargeGunNotConnectPage;
    GUN_STATE[focus_gun_-1].VINflag = 0;
    countdown[focus_gun_-1]->CountDownStop();
    Refreash_Thread[focus_gun_-1].swipCardflag = 0;
    Refreash_Thread[focus_gun_-1].appcharge = 0;
    Refreash_Thread[focus_gun_-1].VINcharge = 0;
    Refreash_Thread[focus_gun_-1].swipcard_YCT = 0;
    if(!Refreash_Thread[focus_gun_-1].ErrorTimer->isActive())
        Refreash_Thread[focus_gun_-1].ErrorTimer->start(300);

    for(int i=0; i< GUN_AMOUNT; i++)
    {
        // get gun which is in charging!
        if (((GUN_STATE[i].Page == chargeWatchingDCPage)
            || (GUN_STATE[i].Page == chargeWatchingACPage))
            && (charging_gun >= GUN_AMOUNT))
        {
            charging_gun = i;
        }
    }

    // jump to charging info page!
    if (charging_gun < GUN_AMOUNT)
    {
        focus_gun_ = charging_gun + 1;  // select charging gun!
        stackLayout->setCurrentIndex(GUN_STATE[charging_gun].Page);
    }
}

void RetrunChargeGun::on_pushButton_clicked()
{
    Init_Focus();
}
